package com.youdelu.road;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.youdelu.road.bean.Attr;
import com.youdelu.road.bean.BgConf;
import com.youdelu.road.utils.BroadcastManager;
import com.youdelu.road.utils.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import me.iwf.photopicker.PhotoPicker;

public class AddMapBgActivity extends AppCompatActivity implements  View.OnClickListener{

    public static final  String SELECT_POSITION = "select_postion";

    private Context mContext;
    private TextView close;
    private EditText name ;
    private Button image ;
    private TextView cancel,check,title,bgtext;
    private String icon_path = null;
    private int id = -1;
    private Gson gson ;
    private float tempX,tempY;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_mapbg);
        mContext = this ;

        close = findViewById(R.id.close);
        name = findViewById(R.id.name);
        name.setFocusable(true);
        name.setFocusableInTouchMode(true);
        name.clearFocus();//失去焦点
        image = findViewById(R.id.image);
        cancel = findViewById(R.id.cancel);
        check = findViewById(R.id.check);
        title = findViewById(R.id.title);
        gson = new Gson();
        Intent intent = getIntent();
        id = intent.getIntExtra("id",-1);
        String bgconf = intent.getStringExtra("bgconf");
        BgConf bgConf = gson.fromJson(bgconf,BgConf.class);
        if(id!=-1){
            title.setText("添加位置");
            image.setVisibility(View.GONE);
            TextView textView = findViewById(R.id.bgtitle);
            textView.setText("当前位置");
            bgtext = findViewById(R.id.bgtext);
            bgtext.setText(bgConf.getScrollX()+","+bgConf.getScrollY()+","+bgConf.getScale()+","+bgConf.getRotate());
        }else{
            TextView textView = findViewById(R.id.bgtitle);
            textView.setText("提示");
            image.setVisibility(View.GONE);
            bgtext = findViewById(R.id.bgtext);
            bgtext.setText("将保存当前方案并新增一个空白方案");
        }
        String mName = intent.getStringExtra("name");
        if(mName!=null){
            name.setText(mName);
        }
        close.setOnClickListener(this);
        cancel.setOnClickListener(this);
        check.setOnClickListener(this);
        //image.setOnClickListener(this);

//        BroadcastManager.getInstance(mContext).addAction(SELECT_POSITION, new BroadcastReceiver() {
//            @Override
//            public void onReceive(Context context, Intent intent) {
//                String str = intent.getStringExtra("attr");
//                if(str!=null){
//                    Attr attr = gson.fromJson(str,Attr.class);
//                    tempX = (float) attr.getPointX();
//                    tempY =(float) attr.getPointY();
//                    bgtext.setText("["+tempX+","+tempY+"]");
//                }
//            }
//        });
    }

    /**
     * 打开图片
     */
    public void openSelect(){
        PhotoPicker.builder()
                .setPhotoCount(1)
                .setShowCamera(false)
                .setShowGif(true)
                .setPreviewEnabled(false)
                .start(this, PhotoPicker.REQUEST_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                String path = photos.get(0);
                icon_path = Util.copyFile(mContext,path);
                image.setText("已上传");
            }
        }
    }

    public void openPosition(){
        Intent intent = new Intent();
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.close:finish();break;
            case R.id.cancel:
                    finish();
                break;
            case R.id.image:
                if(id!=-1){
                    openPosition();
                }else{
                    openSelect();
                }
                break;
            case R.id.check:
                String mName = name.getText().toString();
                if(mName!=null&&!"".equals(mName)){
                    if(id==-1){
//                        if(icon_path==null) {
//                            Toast.makeText(mContext, "请上传地图文件", Toast.LENGTH_SHORT).show();
//                        }else{
                            BroadcastManager.getInstance(mContext).sendBroadcast(MapBgListActivity.SAVE_BG_IMG,id,mName,mName);
                            finish();
                        //}
                    }else{
                       /* if(tempX==0&&tempY==0){
                            Toast.makeText(mContext,"请选择地图位置",Toast.LENGTH_SHORT).show();
                        }else{*/
                            Attr attr = new Attr();
                            attr.setPointX(tempX);
                            attr.setPointY(tempY);
                            attr.setTitle(mName);
                            Log.e("SAVE_POSITION_IMG",gson.toJson(attr));
                            BroadcastManager.getInstance(mContext).sendBroadcast(PointListActivity.SAVE_POSITION_IMG,attr);
                            finish();
                       // }
                    }
                }else{
                    Toast.makeText(mContext,"请输入名称",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
